package com.zjj.edu.web.controller.system;

import com.zjj.edu.common.annotation.Log;
import com.zjj.edu.common.constant.UserConstants;
import com.zjj.edu.common.core.controller.BaseController;
import com.zjj.edu.common.core.domain.AjaxResult;
import com.zjj.edu.common.core.page.TableDataInfo;
import com.zjj.edu.common.core.text.Convert;
import com.zjj.edu.common.enums.BusinessType;
import com.zjj.edu.common.utils.poi.ExcelUtil;
import com.zjj.edu.framework.util.ShiroUtils;
import com.zjj.edu.framework.util.UserPermissionUtil;
import com.zjj.edu.system.domain.SysGrade;
import com.zjj.edu.system.domain.SysUser;
import com.zjj.edu.system.service.ISysGradeService;
import com.zjj.edu.system.service.ISysTenantService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 年级Controller
 *
 * @author zjj
 * @date 2020-06-02
 */
@Controller
@RequestMapping("/system/grade")
public class SysGradeController extends BaseController
{
    private String prefix = "system/grade";

    @Autowired
    private ISysGradeService sysGradeService;

    @Autowired
    private ISysTenantService tenantService;

    @RequiresPermissions("system:grade:view")
    @GetMapping()
    public String grade(ModelMap mmap)
    {
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/grade";
    }

    /**
     * 查询年级列表
     */
    @RequiresPermissions("system:grade:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysGrade sysGrade)
    {
        startPage();
        sysGrade.setTenantId(ShiroUtils.getTenantId());
        List<SysGrade> list = sysGradeService.selectSysGradeList(sysGrade);
        return getDataTable(list);
    }

    /**
     * 导出年级列表
     */
    @RequiresPermissions("system:grade:export")
    @Log(title = "年级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysGrade sysGrade)
    {
        sysGrade.setTenantId(ShiroUtils.getTenantId());
        List<SysGrade> list = sysGradeService.selectSysGradeList(sysGrade);
        ExcelUtil<SysGrade> util = new ExcelUtil<SysGrade>(SysGrade.class);
        return util.exportExcel(list, "grade");
    }

    /**
     * 新增年级
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/add";
    }

    /**
     * 新增保存年级
     */
    @RequiresPermissions("system:grade:add")
    @Log(title = "年级", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysGrade sysGrade)
    {
        sysGrade.setTenantId(ShiroUtils.getTenantId());
        if (UserConstants.NAME_NOT_UNIQUE.equals(sysGradeService.checkNameUnique(sysGrade))) {
            return error("新增年级'" + sysGrade.getName() + "'失败，名称已存在");
        }
        sysGrade.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(sysGradeService.insertSysGrade(sysGrade));
    }

    /**
     * 校验名称
     */
    @PostMapping("/checkNameUnique")
    @ResponseBody
    public String checkTenantNameUnique(SysGrade sysGrade) {
        sysGrade.setTenantId(ShiroUtils.getTenantId());
        return sysGradeService.checkNameUnique(sysGrade);
    }

    /**
     * 修改年级
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("sysGrade", sysGradeService.selectSysGradeById(id));
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/edit";
    }

    /**
     * 修改保存年级
     */
    @RequiresPermissions("system:grade:edit")
    @Log(title = "年级", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysGrade sysGrade)
    {
        sysGrade.setTenantId(ShiroUtils.getTenantId());
        if (UserConstants.NAME_NOT_UNIQUE.equals(sysGradeService.checkNameUnique(sysGrade))) {
            return error("修改年级'" + sysGrade.getName() + "'失败，名称已存在");
        }
        sysGrade.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(sysGradeService.updateSysGrade(sysGrade));
    }

    /**
     * 删除年级
     */
    @RequiresPermissions("system:grade:remove")
    @Log(title = "年级", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        SysUser user = ShiroUtils.getSysUser();
        SysGrade grade = new SysGrade();
        grade.setIds(Convert.toStrArray(ids));
        grade.setUpdateBy(user.getUserName());
        if (!UserPermissionUtil.checkUserPermiss()) {
            grade.setTenantId(user.getTenantId());
        }
        return toAjax(sysGradeService.deleteSysGradeByIds(grade));
    }

    /**
     * 状态修改
     */
    @Log(title = "年级管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:grade:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysGrade sysGrade)
    {
        sysGrade.setTenantId(ShiroUtils.getTenantId());
        return toAjax(sysGradeService.updateSysGrade(sysGrade));
    }

}
